package Control;

import Database.*;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;

import Model.*;

public class EditProduct extends JFrame implements ActionListener
{
	private JLabel id, products, jlDes, jlCost, jlPrice, jlReorder, jlAmount,
			jlDate;
	private JButton search, save, cancel, clear;
	private JTextField jtID, jtDes, jtCost, jtPrice, jtReorder, jtAmount,
			jtDate;
	private JTextArea names;
	private JPanel leftPanel, rightPanel, logo;
	private TableOperations to;
	private JTable jt;
	private DefaultTableModel jtM;
	private ArrayList users;
	private ArrayList<Product> test = new ArrayList<Product>();
	private Product toBeEdited = null;
	private ImageIcon icon = new ImageIcon("frameIcon.png");

	public EditProduct(TableOperations to)
	{
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints gc = new GridBagConstraints();

		setIconImage(icon.getImage());
		setTitle("MDL - Edit Product");
		setSize(750, 320);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLayout(new GridLayout(1, 2));

		this.to = to;
		to.queryProducts();
		Insets insets = new Insets(15, 15, 13, 3);

		// leftPanel
        leftPanel = new JPanel();
        leftPanel.setLayout(layout);

        id = new JLabel("ID or Description");
        gc.gridx = 0;
        gc.gridy = 0;
        gc.weighty = 0;
        gc.weightx = 0;
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        leftPanel.add(id, gc);

        jtID = new JTextField(10);
        gc.gridx = 0;
        gc.gridy = 0;
        gc.weighty = 0;
        gc.weightx = 1;
        insets = new Insets(15, 125, 13, 15);
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        leftPanel.add(jtID, gc);

        search = new JButton("Search");
        search.addActionListener(this);
        gc.gridx = 0;
        gc.gridy = 1;
        gc.weighty = 0;
        gc.weightx = 1;
        insets = new Insets(0, 150, 0, 125);
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        leftPanel.add(search, gc);

        clear = new JButton("Clear");
        clear.addActionListener(this);
        gc.gridx = 0;
        gc.gridy = 1;
        gc.weighty = 0;
        gc.weightx = 1;
        insets = new Insets(0, 250, 0, 25);
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        leftPanel.add(clear, gc);

        products = new JLabel("Products");
        gc.gridx = 0;
        gc.gridy = 2;
        gc.weighty = 0;
        gc.weightx = 0;
        insets = new Insets(0, 50, 10, 3);
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        leftPanel.add(products, gc);

        createTable();
        gc.gridx = 0;
        gc.gridy = 3;
        gc.weighty = 1;
        gc.weightx = 3;
        insets = new Insets(10, 25, 20, 30);
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        leftPanel.add(new JScrollPane(jt), gc);

        add(leftPanel);

        insets = new Insets(15, 15, 5, 3);
        // Right Panel
        rightPanel = new JPanel();
        rightPanel.setLayout(layout);

        jlDes = new JLabel("Description");
        gc.gridx = 0;
        gc.gridy = 1;
        gc.weighty = 0;
        gc.weightx = 0;
        insets = new Insets(15, 75, 5, 30);
        gc.insets = insets;
        rightPanel.add(jlDes, gc);

        jtDes = new JTextField();
        gc.gridx = 2;
        gc.gridy = 1;
        gc.weighty = 0;
        gc.weightx = 1;
        insets = new Insets(15, -125, 5, 3);
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        rightPanel.add(jtDes, gc);

        jlCost = new JLabel("Cost");
        gc.gridx = 0;
        gc.gridy = 3;
        gc.weighty = 0;
        gc.weightx = 0;
        insets = new Insets(15, 75, 5, 30);
        gc.insets = insets;
        rightPanel.add(jlCost, gc);

        jtCost = new JTextField();
        gc.gridx = 2;
        gc.gridy = 3;
        gc.weighty = 0;
        gc.weightx = 1;
        insets = new Insets(15, -125, 5, 3);
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        rightPanel.add(jtCost, gc);

        jlPrice = new JLabel("Price");
        gc.gridx = 0;
        gc.gridy = 5;
        gc.weighty = 0;
        gc.weightx = 0;
        insets = new Insets(15, 75, 5, 30);
        gc.insets = insets;
        rightPanel.add(jlPrice, gc);

        jtPrice = new JTextField();
        gc.gridx = 2;
        gc.gridy = 5;
        gc.weighty = 0;
        gc.weightx = 1;
        insets = new Insets(15, -125, 5, 3);
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        rightPanel.add(jtPrice, gc);

        jlReorder = new JLabel("Reorder level");
        gc.gridx = 0;
        gc.gridy = 7;
        gc.weighty = 0;
        gc.weightx = 0;
        insets = new Insets(15, 75, 5, 30);
        gc.insets = insets;
        rightPanel.add(jlReorder, gc);

        jtReorder = new JTextField();
        gc.gridx = 2;
        gc.gridy = 7;
        gc.weighty = 0;
        gc.weightx = 1;
        insets = new Insets(15, -125, 5, 3);
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        rightPanel.add(jtReorder, gc);
        
        jlAmount = new JLabel("Reorder Amount");
        gc.gridx = 0;
        gc.gridy = 9;
        gc.weighty = 0;
        gc.weightx = 1;
        insets = new Insets(15, 75, 5, 30);
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        rightPanel.add(jlAmount, gc);
        
        jtAmount = new JTextField();
        gc.gridx = 2;
        gc.gridy = 9;
        gc.weighty = 0;
        gc.weightx = 1;
        insets = new Insets(15, -125, 5, 3);
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        rightPanel.add(jtAmount, gc);

        jlDate = new JLabel("Date");
        gc.gridx = 0;
        gc.gridy = 11;
        gc.weighty = 0;
        gc.weightx = 0;
        insets = new Insets(15, 75, 5, 30);
        gc.insets = insets;
        rightPanel.add(jlDate, gc);

        jtDate = new JTextField();
        jtDate.setEditable(false);
        gc.gridx = 2;
        gc.gridy = 11;
        gc.weighty = 0;
        gc.weightx = 1;
        insets = new Insets(15, -125, 5, 3);
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        rightPanel.add(jtDate, gc);

        save = new JButton("Save Changes");
        save.addActionListener(this);
        gc.gridx = 0;
        gc.gridy = 13;
        gc.weighty = 0;
        gc.weightx = 1;
        insets = new Insets(15, 50, 5, 75);
        gc.insets = insets;
        rightPanel.add(save, gc);

        cancel = new JButton("Cancel");
        cancel.addActionListener(this);
        gc.gridx = 2;
        gc.gridy = 13;
        gc.weighty = 0;
        gc.weightx = 1;
        insets = new Insets(15, -65, 5, 3);
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        rightPanel.add(cancel, gc);

        gc.gridx = 3;
        gc.gridy = 2;
        gc.weighty = 1;
        gc.weightx = 1;
        this.add(rightPanel, gc);

	}

	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == cancel)
			this.dispose();
		else if (e.getSource() == search)
		{
			if (jtM.getRowCount() > 0)
			{
				clear();
				search();
			} else
				search();
		} else if (e.getSource() == clear)
			clear();
		else if (e.getSource() == save)
		{
			if (jtDes.getText().equals("") || jtCost.getText().equals("") || jtPrice.getText().equals("") || jtReorder.getText().equals("") || jtAmount.getText().equals("")
                    || Double.parseDouble(jtCost.getText()) <= 0|| Double.parseDouble(jtPrice.getText()) <= 0)
				JOptionPane.showMessageDialog(null, "Check Blank Fields AND Cost/Price equal OR less than 0");
			else if (Double.parseDouble(jtCost.getText()) <= 0
					|| Double.parseDouble(jtPrice.getText()) <= 0)
				JOptionPane.showMessageDialog(null,
						"Product cost and price must be greater than 0");
			else
			{
				String des = jtDes.getText();
				double cost = Double.parseDouble(jtCost.getText());
				double price = Double.parseDouble(jtPrice.getText());

				toBeEdited.setDescription(des);
				toBeEdited.setCost(cost);
				toBeEdited.setPrice(price);

				if (to.updateProduct(toBeEdited))
				{
					JOptionPane.showMessageDialog(null, "Product updated");
					clear();
					this.dispose();
				} else
					JOptionPane.showMessageDialog(null, "Update Unsuccessful");
			}
		}
	}

	public void createTable()
	{
		jtM = new DefaultTableModel();

		jtM.addColumn("Product Name");

		fillTable();

		jt = new JTable(jtM);
		jt.addMouseListener(new MouseAdapter()
		{
			public void mousePressed(MouseEvent e)
			{
				SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yy");
				int row = jt.getSelectedRow();
				int productID = test.get(row).getId();

				toBeEdited = to.populateProduct(productID);
				jtDes.setText(toBeEdited.getDescription());
				jtCost.setText(Double.toString(toBeEdited.getCost()));
				jtPrice.setText(Double.toString(toBeEdited.getPrice()));
				jtReorder.setText(Integer.toString(toBeEdited.getReorder()));
				Date d = toBeEdited.getSqlDate();
				jtDate.setText(format.format(d));
			}
		});
	}

	public void fillTable()
	{
		if (jtM.getRowCount() >= 1)
		{
			clear();

			String[] name =
			{ test.get(test.size() - 1).getDescription() };
			jtM.addRow(name);

		} else
		{
			for (int i = 0; i < test.size(); i++)
			{
				String[] name =
				{ test.get(i).getDescription() };
				jtM.addRow(name);
			}
		}
	}

	public void clear()
	{
		try
		{
			jtM.removeRow(0);
			for (int i = 0; i < jtM.getRowCount(); i++)
			{
				jtM.removeRow(i);
			}

			test.remove(0);
			for (int i = 0; i < test.size(); i++)
			{
				test.remove(i);
			}
		} catch (Exception e)
		{
			System.out.println("No rows to be deleted");
		} finally
		{
			// jtID.setText("");
			jtDes.setText("");
			jtCost.setText("");
			jtPrice.setText("");
			jtReorder.setText("");
			jtDate.setText("");
		}
	}

	public void search()
	{
		try
		{
			test.addAll(to.searchProduct(Integer.parseInt(jtID.getText())));
		} catch (NumberFormatException nfe)
		{
			test.addAll(to.searchProduct(jtID.getText()));
		}
		fillTable();
		if (jtM.getRowCount() <= 0)
			JOptionPane
					.showMessageDialog(null,
							"Product not found. Hint: Use a CAPITAL LETTER for the 1st Letter");
	}
}